﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using IndieStudio.BugsBang.Utility;

///Developed By Indie Studio
///https://assetstore.unity.com/publishers/9268
///www.indiestd.com
///info@indiestd.com

namespace IndieStudio.BugsBang.Game
{
	[DisallowMultipleComponent]
	public class Settings : MonoBehaviour
	{
		/// <summary>
		/// The music slider reference.
		/// </summary>
		public Slider musicSlider;
				
		/// <summary>
		/// The sfx slider reference.
		/// </summary>
		public Slider sfxSlider;

		/// <summary>
		/// Remove ads button.
		/// </summary>
		public Button removeAdsButton;

		/// <summary>
		/// The vibration button.
		/// </summary>
		public Transform vibrationButton;

		/// <summary>
		/// The sprites of the green and red buttons.
		/// </summary>
		public Sprite[] greenRedButtons;

		/// <summary>
		/// Whether the vibration is enabled or not.
		/// </summary>
		public static bool vibrationEnabled;

		// Use this for initialization
		void Start ()
		{
			SetMusicValue (AudioSources.instance.MusicAudioSource ().mute);
			SetSFXValue (AudioSources.instance.SFXAudioSource ().mute);

			/*
			if (true) {
				Debug.Log ("TO DO: Disable ads button if Remove Ads is purchased");
				removeAdsButton.interactable = false;
			}
			*/

			SetVibrationState (vibrationButton);
		}

		/// <summary>
		/// On music slider change event.
		/// </summary>
		public void OnMusicSliderChange ()
		{
			SetMusicValue (musicSlider.value == 1 ?  false : true);
		}

		/// <summary>
		/// On sfx slider change event.
		/// </summary>
		public void OnSFXSliderChange ()
		{
			SetSFXValue (sfxSlider.value == 1 ? false : true);
		}

		/// <summary>
		/// Toggle the state of the vibration.
		/// </summary>
		public void ToggleVibrationState (Transform button)
		{
			vibrationEnabled = !vibrationEnabled;
			if (vibrationEnabled) {
				GameManager.Vibrate ();
			}
			SetVibrationState (button);
			DataManager.SaveVibrationStatus (vibrationEnabled);
		}

		/// <summary>
		/// Set the state of the vibration.
		/// </summary>
		public void SetVibrationState (Transform button)
		{
			if (vibrationEnabled) {
				button.transform.Find ("TitleText").GetComponent<Text> ().text = "震动开启";
				button.GetComponent<Image> ().sprite = greenRedButtons[0];
			} else {
				button.transform.Find ("TitleText").GetComponent<Text> ().text = "震动关闭";
				button.GetComponent<Image> ().sprite = greenRedButtons[1];
			}
		}

		/// <summary>
		/// Set up the music value.
		/// </summary>
		/// <param name="mute">whether to mute or not.</param>
		private void SetMusicValue (bool mute)
		{
			AudioSources.instance.MusicAudioSource ().mute = mute;
			musicSlider.value = (mute == true ? 0 : 1);
			DataManager.SaveMusicMuteValue(mute == true ? 1 : 0);
		}

		/// <summary>
		/// Set up the SFX value.
		/// </summary>
		/// <param name="value">Value.</param>
		private void SetSFXValue (bool mute)
		{
			AudioSources.instance.SFXAudioSource ().mute = mute;
			sfxSlider.value = (mute == true ? 0 : 1);
			DataManager.SaveSFXMuteValue( mute == true ? 1 : 0);
		}
	}
}
